package llk.views
{
	import flash.display.Sprite;
	import flash.utils.Dictionary;
	
	import llk.GameModel;
	import llk.controllers.LlkController;
	import llk.datas.vo.ItemVO;
	import llk.views.object.ItemView;

	/**
	 * 游戏主场景
	 **/
	public class LLKGamLayerView extends Sprite
	{
		private var _itemList:Array = GameModel.getInstance( ).itemList;
		private var itemViewList:Array;
		private var _ctr:LlkController;
		private var _dic:Dictionary;
		public function LLKGamLayerView($ctr:LlkController)
		{
			super();
			_ctr = $ctr;
			_dic = new Dictionary;
		}
		
		public function stopCreateRandomView():void{
			//更新ID 
			for each(var item:ItemView in itemViewList){
				if(item.itemVO){
					if(item.itemVO.id == 2){
						item.itemVO.id = 1
					}
				}
			}
		}
		
		public function updataView(vo1:ItemVO,vo2:ItemVO ):void{
			//根据itemList更新itemViewList
			var temp:String = String(vo1.row)+String(vo1.col);
			itemViewList[int(temp)].itemVO = vo1;
			itemViewList[int(temp)].show();
			temp = String(vo2.row)+String(vo2.col);
			itemViewList[int(temp)].itemVO = vo2;
			itemViewList[int(temp)].show();
//			GameModel.getInstance().itemList[vo1.row][vo1.col] = vo1;
//			GameModel.getInstance().itemList[vo2.row][vo2.col] = vo2;
		}
		private var idCount:int = 0;
		public function creatView( ):void {//根据ROW/COL创建数组
			itemViewList = [];
			for(var i:int = 0;i<_itemList.length;i++){
				for(var j:int = 0;j<_itemList[0].length;j++){
					var itemView:ItemView = new ItemView(_ctr);
					var temp:String = String(_itemList[i][j].row)+String(_itemList[i][j].col);
					itemViewList[int(temp)] = itemView;
					itemView.move(i*35,j*35);
					addChild(itemView);
					if(_itemList[i][j].id == 1){
						itemView.itemVO = _itemList[i][j]
						itemView.show();
//						itemViewList.push(itemView);
						_dic[temp] = temp;
					}
				idCount++;
				}
			}
			GameModel.getInstance().itemVODic = _dic;
			GameModel.getInstance().idCount = idCount;
		}
		public function removeItem( $item:ItemView ):void {
			$item.destroy( );
		}
		
		public function getItemByVO( itemVO:ItemVO ):ItemView {
			for each(var item:ItemView in itemViewList){
				if(item.itemVO == itemVO){
					return item
				}
			}
				return null
		}
		 
		
	}
}